package com.song.service.impl;
import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.google.gson.Gson;
import com.song.config.ContantsProperties;
import com.song.service.MsmService;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.Map;
/**
 * @program: untitled1
 * @ClassName MsmServiceImpl
 * @description:
 * @author: 刘庆东
 * @create: 2025−01-08 15:15
 * @Version 1.0
 **/
@Service
public class MsmServiceImpl implements MsmService {
    @Override
    public boolean send(String phone, String code) {
        if(StringUtils.isEmpty(phone)){
            return false;
        }

        //设置发送短信的参数
        DefaultProfile profile = DefaultProfile.
                getProfile(ContantsProperties.REGION_ID,
                        ContantsProperties.ACCESS_KEY_ID,
                        ContantsProperties.ACCESSKEY_SSECRET);

        //发送短信的客户端
        IAcsClient client = new DefaultAcsClient(profile);
        //设置客户端参数
        CommonRequest request = new CommonRequest();
        request.setMethod(MethodType.POST);//发送方式
        request.setDomain("dysmsapi.aliyuncs.com");
        request.setVersion("2017-05-25");
        request.setAction("SendSms");
        //设置发送手机号
        request.putQueryParameter("PhoneNumbers",phone);
        //签名
        request.putQueryParameter("SignName","令狐冲");
        //设置发送短信儿模板
        request.putQueryParameter("TemplateCode","SMS_478250111");


        Map<String,Object> param = new HashMap<>();
        param.put("code",code);
        request.putQueryParameter("TemplateParam", JSONObject.toJSONString(param));


        try {
            CommonResponse response = client.getCommonResponse(request);
            boolean success = response.getHttpResponse().isSuccess();

            return success;
        } catch (ClientException e) {
            e.printStackTrace();
        }

        return false;
    }

    @Override
    public boolean send(String phone, String code, Map<String, Object> paramMap) {
        if(StringUtils.isEmpty(phone)){
            return false;
        }

        //设置发送短信的参数
        DefaultProfile profile = DefaultProfile.
                getProfile(ContantsProperties.REGION_ID,
                        ContantsProperties.ACCESS_KEY_ID,
                        ContantsProperties.ACCESSKEY_SSECRET);

        //发送短信的客户端
        IAcsClient client = new DefaultAcsClient(profile);
        //设置客户端参数
        CommonRequest request = new CommonRequest();
        request.setMethod(MethodType.POST);//发送方式
        request.setDomain("dysmsapi.aliyuncs.com");
        request.setVersion("2017-05-25");
        request.setAction("SendSms");
        //设置发送手机号
        request.putQueryParameter("PhoneNumbers",phone);
        //签名
        request.putQueryParameter("SignName","令狐冲");
        //设置发送短信儿模板
        request.putQueryParameter("TemplateCode","SMS_478250111");


/*        Map<String,Object> param = new HashMap<>();
        param.put("code",code);*/

        Gson gson = new Gson();

        String jsonParam = gson.toJson(paramMap);

        request.putQueryParameter("TemplateParam", JSONObject.toJSONString(jsonParam));



        try {
            CommonResponse response = client.getCommonResponse(request);
            boolean success = response.getHttpResponse().isSuccess();

            return success;
        } catch (ClientException e) {
            e.printStackTrace();
        }

        return false;
    }
}
